﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SimpleSfxGUI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.templateEXEPath.Text = Environment.CurrentDirectory + "\\Unpacker.exe";
        }

        private void templateEXEBrowse_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
            ofd.Filter = "EXE files|*.exe";
            ofd.Multiselect = false;
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.templateEXEPath.Text = ofd.FileName;
            }
        }

        private void sourceZIPBrowse_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
            ofd.Filter = "ZIP files|*.zip";
            ofd.Multiselect = false;
            ofd.RestoreDirectory = true;
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.sourceZIPPath.Text = ofd.FileName;
            }
        }

        private void createButton_Click(object sender, RoutedEventArgs e)
        {
            //build the self-extractor
            System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
            sfd.Filter = "EXE files|*.exe";
            sfd.RestoreDirectory = true;
            if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    SimpleSfxLib.SFXUtility.BuildSFX(sourceZIPPath.Text, templateEXEPath.Text, sfd.FileName);
                }
                catch (Exception x)
                {
                    MessageBox.Show(this, "An error occurred: " + x.ToString(), "SimpleSFX");
                }
                MessageBox.Show(this, "Self-extractor EXE was created successfully.", "SimpleSFX");
            }
        }

        private void Hyperlink_Click(object sender, RoutedEventArgs e)
        {
            Web("http://flexsetup.codeplex.com/documentation");
        }

        public static void Web(string url)
        {
            System.Diagnostics.Process p = Start(url);
            if (p != null)
            {
                p.Close();
            }
        }
        public static System.Diagnostics.Process Start(string path)
        {
            System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(path);
            psi.UseShellExecute = true;
            return System.Diagnostics.Process.Start(psi);
        }
    }
}
